package com.bysj.lms.controller;


import com.bysj.lms.common.ResultModel;
import com.bysj.lms.pojo.entity.LmsCheck;
import com.bysj.lms.pojo.entity.LmsLve;
import com.bysj.lms.service.ILmsCheckService;
import com.bysj.lms.service.ILmsLveService;
import com.bysj.lms.utils.EasyExcelUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-02-04
 */
@Api(tags = "低值设备")
@RestController
@RequestMapping("/lms-lve")
public class LmsLveController {


    @Autowired
    ILmsLveService lmsLveService;

    @Autowired
    ILmsCheckService lmsCheckService;
    
    @ApiOperation(value = "仅用于swagger显示模型")
    @GetMapping("")
    public LmsLve getModel(){
        return new LmsLve();
    }


    @ApiOperation(value = "查询设备",notes = "name为空查询所有低值设备，name不为空模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页"),
            @ApiImplicitParam(name = "count",value = "页面的大小"),
            @ApiImplicitParam(name = "name",value = "设备名")
    })
    @GetMapping("/find")
    public ResultModel find(Integer page,Integer count,String name){

        PageInfo pageInfo = lmsLveService.findByPage(name, page, count);
        return ResultModel.success("查询成功",pageInfo);
    }

    @ApiOperation(value = "修改设备")
    @PutMapping("/update")
    public ResultModel update(@RequestBody LmsLve lmsLve){

        boolean update = lmsLveService.updateById(lmsLve);
        if(update){
            return ResultModel.success("修改成功");
        }
        return ResultModel.failure("修改失败");
    }

    @ApiOperation(value = "新增低值设备（单个）")
    @PostMapping("/add")
    public ResultModel addOne(@RequestBody LmsLve lmsLve){
        if(!lmsLveService.isExist(lmsLve.getInstruNumber())){
            boolean save = lmsLveService.save(lmsLve);
            if(save){
                return ResultModel.success("添加成功");
            }
            return ResultModel.failure("添加失败");
        }
        return ResultModel.failure("仪器编号已存在");
    }

    @PostMapping("/adds")
    @ApiOperation(value = "批量添加设备", notes = "导入设备表")
    public ResultModel adds(@RequestParam("excel") MultipartFile excel){
    
        List<LmsLve> lmsLves = null;
        try {
            lmsLves = EasyExcelUtil.read(excel, LmsLve.class);
            boolean save = lmsLveService.adds(lmsLves);
            if(save){
                return ResultModel.success("添加成功");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultModel.failure("添加失败");
    }

    @GetMapping("/exportExcel")
    @ApiOperation(value = "导出excel表")
    public void exportExcel(HttpServletResponse response){
        List<LmsLve> list = lmsLveService.list();
        if(list.size()==0) {list.add(new LmsLve());}
        try {
            EasyExcelUtil.write(response,"低值设备","低值设备", LmsLve.class,list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "根据id删除设备")
    public ResultModel delete(Integer id){
        boolean delete = lmsLveService.removeById(id);
        if(delete){
            return ResultModel.success("删除成功");
        }
        return ResultModel.failure("删除失败");
    }



    @ApiOperation("绑定实验室")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "number",value = "低值设备编号"),
            @ApiImplicitParam(name = "rid",value = "实验室id")
    })
    @PutMapping("/bind")
    public ResultModel bind(String number,Integer rid){
        boolean bind = lmsLveService.bind(number, rid);
        if(bind) {
            lmsCheckService.lambdaUpdate().set(LmsCheck::getRoomId, rid)
                    .eq(LmsCheck::getInstruType, 1).eq(LmsCheck::getInstruNumber, number)
                    .update();
            return ResultModel.success("绑定成功");}
        return ResultModel.failure("绑定失败");
    }
}

